home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 283_01 / keyin.c < prev    next >
C/C++ Source or Header  |  1988-09-08  |  5KB  |  215 lines

  1. /* keyin.c */
  2.  
  3. #include "ciao.h"
  4. #include <dos.h>
  5. #include <stdarg.h>
  6.  
  7.  
  8.  
  9.  
  10. /*=============================*/
  11. /* keyin( screenwait )         */
  12. /*                             */
  13. /* screen goes blank after 8   */
  14. /* minutes of inactivity       */
  15. /*=============================*/
  16.  
  17.  
  18. void screenwait( void )
  19. {
  20.      extern int vid_attr;
  21.  
  22.      static int screentime = 0;
  23.      char far *p;
  24.      union REGS x;
  25.      int h,m,s,d;
  26.  
  27.      acg();  /* bump the random number generator whilst here */
  28.  
  29.      if ( !screentime )
  30.      {
  31.          get_system_time( &h, &m, &s, &d );
  32.          screentime = (h * 60) + m;
  33.          screentime += 8;  /* eight minutes from now */
  34.      }
  35.  
  36.      get_system_time( &h, &m, &s, &d );
  37.      h = (h * 60) + m;
  38.      if ( h > screentime ) /* blank screen after eight minutes of inactivity */
  39.      {
  40.          screentime = 0;
  41.          p = savescreen( &x );
  42.          fullscreen();                /* ensure entire screen is blanked out */
  43.          vid_attr = 7;              /* ensure screen is BLACK  */
  44.          setcursize( 32,32 );       /* and cursor is INVISIBLE */
  45.          gotoxy(0,0);
  46.          rptchar(' ',2000);
  47.          while ( !kbhit() )
  48.          ;
  49.          while ( kbhit() ) getch();
  50.          restorescreen( p, &x );  /* put screen back when user hits a key */
  51.      }
  52.      if ( kbhit() ) screentime = 0;  /* keyin will find the waiting char */
  53. }
  54.  
  55.  
  56. /* additive congruential generator for pseudo-random numbers
  57. ** see Knuth, Art of Computer Programming, Vol. 2, p.27
  58. */
  59.  
  60. #define ACG_SIZE 55  /* this is a *MAGIC* number, do not modify! */
  61.  
  62. static unsigned acgnum[ACG_SIZE] = {
  63.      44292,    60,     4947,     3972,     4489,
  64.      1917,     23916,  7579,     3048,     56856,
  65.      11832,    7589,   1798,     4954,     2880,
  66.      5142,     5187,   3045,     31529,    3110,
  67.      4333,     167,    5556,     7237,     5906,
  68.      5419,     56632,  15833,    3760,     1081,
  69.      1434,     80,     6212,     344,      7303,
  70.      3044,     7675,   5420,     19457,    33434,
  71.      2657,     700,    6777,     4436,     620,
  72.      42129,    629,    23550,    1639,     4546,
  73.      1220,     6469,   862,      3280,     4664
  74. };
  75.  
  76. unsigned acg( void )
  77. {
  78.  
  79.      static int rp1 = 54, rp2 = 23;  /* these are *MAGIC* numbers! */
  80.  
  81.      rp1++;
  82.      rp2++;
  83.      rp1 %= ACG_SIZE;
  84.      rp2 %= ACG_SIZE;
  85.      acgnum[rp1] += acgnum[rp2];
  86.      return ( acgnum[rp1] );
  87.  
  88. }
  89.  
  90.  
  91. void init_acg( void )  /* used when can't get a keypress first */
  92. {
  93.       unsigned h,m,s,d;
  94.       get_system_time( &h, &m, &s, &d );
  95.       srand( d );
  96.       for ( h = 0; h < ACG_SIZE; h++ )
  97.           acgnum[h] += rand();
  98. }
  99.  
  100.  
  101. /*=============================*/
  102. /* keyin()                     */
  103. /*                             */
  104. /* extended ASCII chars are    */
  105. /* returned << 8 to high byte; */
  106. /* see "keys.h" for definition */
  107. /*=============================*/
  108.  
  109.  
  110. int keyin( void (* wait)( void ) )
  111. {
  112.      int ch;
  113.  
  114.      do
  115.      { 
  116.          (* wait)();   /* while waiting for input */
  117.      } 
  118.      while ( !kbhit() );
  119.  
  120.      /* This appears to be the best algorithm I can find; signal is
  121.         still a problem, but the keyboard does not inexplicably lock up
  122.         when using this method; extended ascii are defined in <keys.h>.
  123.         */
  124.      ch = (getch() & 0xFF);
  125.      return ((ch == 0)? ((getch() & 0xFF) << 8): ch);
  126. }
  127.  
  128.  
  129. int keyshift_status( void )
  130. {
  131.     union REGS x;
  132.     x.h.ah = 2;
  133.     int86( 22, &x,&x );
  134.     return (x.h.al & 0xff);
  135. }
  136.  
  137.  
  138. void flush_keys( void )
  139. {
  140.     while (kbhit()) getch();
  141. }
  142.  
  143.  
  144.  
  145.  
  146. /* message() has variable args, works like wprintf with gotoxy
  147. */
  148.  
  149. void message( int x, int y, char *p, ...)
  150. {
  151.      va_list arg_ptr;
  152.      char *buff;
  153.  
  154.      gotoxy( x,y ); wputs("^0"); clreol();  /* NOTE: Clears line! */
  155.      buff = malloc( 128 );
  156.      va_start(arg_ptr, p);
  157.      vsprintf( buff, p, arg_ptr );
  158.      va_end( arg_ptr );
  159.      wputs( buff );
  160.      free( buff );
  161. }
  162.  
  163.  
  164.  
  165. /*
  166. **  noop definition for #defines in debug test files
  167. */
  168.  
  169. void noop()
  170. {
  171.      ;
  172. }
  173.  
  174.  
  175.  
  176. void bell() { putch('\a'); }
  177.  
  178.  
  179. unsigned int sleep( int tenths )
  180. {
  181.      unsigned h1, h2, m1, m2, s1, s2, d1, d2;
  182.  
  183.      tenths *= 10;  /* convert to hundredths, e.g. 20 * 10 = 200 hundredths */
  184.  
  185.      get_system_time( &h1, &m1, &s1, &d1 );
  186.      h1 = (100 * s1) + d1;  /* e.g., 5910 hundredths of a second */
  187.      do
  188.      {
  189.            if (kbhit()) 
  190.           {
  191.                 return ( keyin( noop ) );  /* return user's keystroke */
  192.           }
  193.           get_system_time( &h2, &m2, &s2, &d2 );
  194.           if (s2 < s1) s2 += 60;
  195.           h2 = ((100 * s2) + d2) - h1; /* e.g., 6030 - 5910 = 120 hundredths */
  196.      }
  197.      while ( h2 < tenths );  /* e.g., while 120 < 200 */
  198.      return (0);
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205. int getkey()
  206. {
  207.       int n;
  208.       n = keyin(screenwait);
  209.       HIclack();
  210.       return (n);
  211. }
  212.  
  213.  
  214.  
  215.